﻿using AutoMapper;
using Entity.BaseInfo.DTO.Supplier;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Supplier;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.BaseInfo
{
    public class SupplierService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public SupplierService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取供应商列表
         */
        public async Task<RequestResponseModel<PageResult<SupplierView>>> GetSupplierList(SupplierSearchDTO supplierSearchDTO)
        {
            //筛选
            var query = _context.SupplierView
                .WhereIF(a => a.Code.Contains(supplierSearchDTO.Code), !string.IsNullOrEmpty(supplierSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(supplierSearchDTO.Name), !string.IsNullOrEmpty(supplierSearchDTO.Name))
                .WhereIF(a => a.IsEnable == supplierSearchDTO.IsEnable, supplierSearchDTO.IsEnable != null)
                ;

            //返回列表
            var supplierList = await query
                .OrderBy(a => a.CreateTime)
                .Page(supplierSearchDTO.CurrentPage, supplierSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<SupplierView>(supplierList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增供应商
         */
        public async Task<RequestResponseModel<string>> AddSupplier(SupplierAddDTO supplierAddDTO)
        {
            //新增数据
            var supplier = _mapper.Map<SupplierModel>(supplierAddDTO);
            supplier.CreateTime = DateTime.Now;
            supplier.Id = Guid.NewGuid().ToString();

            //更新表
            _context.Supplier.Add(supplier);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", supplier.Id);
        }

        /**
         * 更新供应商
         */
        public async Task<RequestResponseModel<string>> PutSupplier(SupplierUpdateDTO supplierUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<SupplierModel>(_context.Supplier, supplierUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", supplierUpdateDTO.Id);
            }

            //编辑数据
            var supplier = _mapper.Map(supplierUpdateDTO, query);

            //更新表
            _context.Supplier.Update(supplier);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", supplier.Id);
        }

        /**
         * 删除供应商
         */
        public async Task<RequestResponseModel<string>> DeleteSupplier(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<SupplierModel>(_context.Supplier, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //更新表
            _context.Supplier.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
